package com.namecard.namecardapi.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.namecard.namecardapi.entity.DTO.CommentDto;
import com.namecard.namecardapi.entity.TComment;
import com.namecard.namecardapi.result.Result;
import com.namecard.namecardapi.service.TCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@DS("slave2")
@Api(tags = "评论")
@RestController
@RequestMapping("/comment")
public class TCommentController {

    @Autowired
    private TCommentService tCommentService;

    //    发表评论
    @ApiOperation("发表评论")
    @PostMapping("/insertComment")
    public Result insertComment(@RequestBody CommentDto commentdto) {
        int i = tCommentService.insertComment(commentdto);
        if (i > 0) {
            return Result.ok();
        }
        return Result.fail("发表评论失败");
    }

    //    删除评论
    @ApiOperation("删除评论")
    @DeleteMapping("/deletcComment")
    public Result deletcComment(@RequestParam Long id) {
        boolean flag = tCommentService.removeById(id);
        if (flag) {
            return Result.ok();
        }
        return Result.fail("删除评论失败");
    }


    //根据商品id查询商品评论
    @ApiOperation(value = "分页查询")
    @GetMapping("/{page}/{limit}/{commodityId}")
    public Result page(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "commodityId", value = "商品id", required = true)
            @PathVariable Long commodityId) {
        Page<TComment> pageParam = new Page<>(page, limit);
        IPage<TComment> pageModel = tCommentService.selectPage(pageParam, commodityId);
        return Result.ok(pageModel);
    }
}
